using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using SlimDX;
using SlimDX.DirectInput;

namespace miciv
{
    public class MouseInput
    {
        protected CoreContext m_coreContext;
        protected Mouse m_mouseInput;
        protected MouseState m_oldState;
        protected bool[] m_JustPressedButtons;
        protected bool[] m_JustReleasedButtons;
        protected bool[] m_PressedButtons;
        protected bool[] m_ReleasedButtons;
        protected int m_X;
        protected int m_Y;
        protected int m_Z;

        public bool[] JustPressedButtons
        {
            get
            {
                return this.m_JustPressedButtons;
            }
        }

        public bool[] JustReleasedButtons
        {
            get
            {
                return this.m_JustReleasedButtons;
            }
        }

        public bool[] PressedButtons
        {
            get
            {
                return this.m_PressedButtons;
            }
        }

        public bool[] ReleasedButtons
        {
            get
            {
                return this.m_ReleasedButtons;
            }
        }

        public int DeltaX
        {
            get
            {
                if (null != this.m_oldState)
                {
                    return this.m_oldState.X;
                }
                return 0;
            }
        }

        public int DeltaY
        {
            get
            {
                if (null != this.m_oldState)
                {
                    return this.m_oldState.Y;
                }
                return 0;
            }
        }

        public int DeltaZ
        {
            get
            {
                if (null != this.m_oldState)
                {
                    return this.m_oldState.Z;
                }
                return 0;
            }
        }

        public int X
        {
            get
            {
                return this.m_X;
            }
            set
            {
                this.m_X = value;
            }
        }

        public int Y
        {
            get
            {
                return this.m_Y;
            }
            set
            {
                this.m_Y = value;
            }
        }

        public int Z
        {
            get
            {
                return this.m_Z;
            }
            set
            {
                this.m_Z = value;
            }
        }

        public MouseInput()
        {
            this.m_JustPressedButtons = new bool[8];
            this.m_JustReleasedButtons = new bool[8];
            this.m_PressedButtons = new bool[8];
            this.m_ReleasedButtons = new bool[8];
            this.m_X = 0;
            this.m_Y = 0;
            this.m_Z = 0;
        }

        public bool Initialize(CoreContext _coreContex)
        {
            CooperativeLevel cl = CooperativeLevel.Foreground | CooperativeLevel.Nonexclusive;
            this.m_coreContext = _coreContex;
            this.m_mouseInput = new Mouse(this.m_coreContext.GetData<DirectInput>("DirectInput"));
            this.m_mouseInput.SetCooperativeLevel(this.m_coreContext.GetData<Form>("Form").Handle, cl);
            this.m_mouseInput.Acquire();
            return true;
        }

        public void Update()
        {
            if ((this.m_mouseInput.Acquire().IsFailure) || (this.m_mouseInput.Poll().IsFailure))
            {
                return;
            }

            MouseState state = this.m_mouseInput.GetCurrentState();
            if ((Result.Last.IsFailure) || (null == this.m_oldState))
            {
                this.m_oldState = state;
                return;
            }

            bool[] buttons = state.GetButtons();
            for (int i = 0 ; (i < buttons.Length) && (8 > i) ; ++i)
            {
                this.m_PressedButtons[i] = (false != buttons[i]);
                this.m_ReleasedButtons[i] = (false == buttons[i]);
                this.m_JustPressedButtons[i] = (false != this.m_oldState.IsReleased(i)) && (false != this.m_PressedButtons[i]);
                this.m_JustReleasedButtons[i] = (false != this.m_oldState.IsPressed(i)) && (false != this.m_ReleasedButtons[i]);
            }

            this.m_oldState = state;
        }
    }
}
